<?php  

class News_Usenet_Article
{
    var $messageId;
    var $Subject;
    var $From;
    var $articleDate;
    var $articleBytes;
    var $articleLines;
    var $Ref;
    var $regEx;
    var $articleList;
    var $partList;
    var $Level;
    var $prefix;
    var $matchKey;
    var $partKey;
    var $groupName;
    function News_Usenet_Article($buf, $level=1, $group='')
    {
        $this->groupName    = $group;
        $this->articleList  = array();
        $this->partList     = array();
        
        $elements           = preg_split("/\t/", $buf);                                            
        $this->messageId    = $elements [0];                                       
        $this->Subject      = $elements [1];                                                 
        $this->From         = decode_sender($elements[2]);                                            
        $this->articleDate  = $elements [3];                                        
        $this->articleBytes = $elements [6];                                                
        $this->articleLines = $elements [7];                                               
        $this->regEx        = $this->_getRegEx();
        $this->partKey      = $this->_getPartKey();
        
        $arr = preg_split("/\s+/", trim($elements [8]));
        $this->Ref = array();
        for ($i=0;$i<sizeof($arr);$i++)
        {
            $sp = explode (':', $arr[$i]);
            if (sizeof($sp) > 1)
            {
                $this->Ref[] = $sp[0];
            }
        }
        
        
    #    print_r ($elements);
    #    $this->Level        = $level;
    #    $this->matchKey     = $this->_Parse($this->Subject);  
    #    $this->prefix       = $this->_getPrefix($buf);
        
        
    }
    
    function _getRegEx()
    { 
        $regex        = preg_replace('/["\-\/\+\[\]\(\)\*]/', '.', $this->Subject);
        $regex        = preg_replace('/(\d+)/', '\d+', $regex);
        $regex        = preg_replace('/(\s+)/', '\s+', $regex); 
        
        if (preg_match('/\.(jpg|png|gif|bmp|wmv|rar|r\d{2})/i', $regex, $arr))
        {
            $regex = substr($regex, 0, strpos($regex, $arr[1]));
        }
        return $regex;
    }
    
    function _getPartKey()
    {
        $reg = '/(.*)\((\d+)\/(\d+)\)/';
        $key = $this->Subject;  
        if (preg_match($reg, $key, $info))
        {
            $key = $info[1]; 
        }
        
        return $key;
    }
    
    
    function _getPrefix($buf)
    {
        $name = $this->_getSubject($buf);
        $pos   = strpos($name, '[')!==false;
        if ($pos)
        {
            return substr($name, 0, $pos);
        }
        return '';
    }
    
    function _getSubject($buf)
    {
        $elements = preg_split("/\t/", $buf);                            
        return $elements [1];         
    }
    
    
    function _Parse($subject)
    {             
        if (preg_match('/(.*)\((\d+)\/(\d+)\)/', $subject, $info))
        {
            return $info[1];
        } 
        return NULL;
    } 
    
    function Match ($buf)
    {                            
   #     if (strlen($this->prefix) > 0)
   #     {    
   #         $prefix = $this->_getPrefix($buf);
   #         if ($prefix == $this->prefix);
   #         return true;
   #     }
   #     return false;
    #    if (strlen($this->matchKey) > 0)
    #    {
    #        $parsed = $this->_Parse($this->_getSubject($buf));
    #        if ($parsed)
    #        { 
    #            if ($parsed == $this->matchKey)
    #            {
    #               # echo "[$parsed]/[$this->matchKey]";
    #                return true;
    #            }
    #        }
    #    }
    #    return false;
        return preg_match ('/' . $this->regEx . '/', $this->_getSubject($buf), $array); 
    }
    
    function AddPart($article, $index)
    {
        $this->partList[$index - 1] = $article;
    }
    
    function Add($buf, $keymatch=0)
    {
        $reg = '/(.*)\((\d+)\/(\d+)\)/';
        $key = $this->_getSubject($buf); 
        $tmp = new News_Usenet_Article($buf);
        $idx = -1;
        $sz  = 1;
        
        if (preg_match($reg, $key, $info))
        {
            $key = $info[1];
            $idx = $info[2];
            $sz  = $info[3];
 #           $tmp->AddPart ($tmp, $idx);
        }
    
     
 
        
        
            
        if (array_key_exists($this->regEx, $this->articleList))
        {
            $article = $this->articleList[$this->regEx];
            if (array_key_exists($key, $article))
            {
                $this->articleList[$this->regEx][$key][] = $tmp;
            }
            else 
            {
                $this->articleList[$this->regEx][$key] = array($tmp);
            }
        }
        else
        {
            $this->articleList[$this->regEx] = array($key=>array($tmp));
        }                      
    }
    
    function _getKey()
    {
        $ret = array();
        $ret[] = $this->messageId;
        if (sizeof($this->articleList) > 0)
        { 
	        foreach ($this->articleList as $item)
	        {
	            if (is_array($item))
	            {
	                foreach ($item as $i) 
	                    foreach ($i as $x)
	                        $ret[] = $x->_getKey(); 
	            }
	            else
	            $ret[] = $item->_getKey();
	        } 
        } 
        return implode('-', $ret);
    }
    
    function _Display()
    {
        $ret = array();
        $link  = '<a href="/nntp/home/image/group/%s/id/%s" target="_blank">%s</a> [' . $this->_getKey() . ']';
      #  $ret[] = '<li>' . $this->Subject . '[' . ($this->Level==0?$this->_getKey():'') . ']';
        $ret[] = '<li>' . sprintf($link, $this->groupName, $this->_getKey(), $this->Subject) . '';
        if (sizeof($this->articleList) > 0)
        {
            $ret[] = '<ul>';
	        foreach ($this->articleList as $item)
	        {
	            if (is_array($item))
	            {
	                foreach ($item as $i) $ret[] = $i[0]->_Display(); 
	            }
	            else
	            $ret[] = $item->_Display(); 
	        }
            $ret[] = '</ul>';
        }
        $ret[] = '</li>';   
        return implode('', $ret);
    }
    
    function xDisplay()
    {
         echo "<h2>{$this->regEx}</h2>";
        echo '<li>' . $this->Subject . '(' . sizeof($this->articleList) . ')';
        
        echo "<xmp>";
        var_dump ($this->articleList);
        echo '</xmp>';
    }
    
    function Display()
    {
       echo $this->_Display();
       return;
       echo '<li>' . $this->Subject;
       if (sizeof($this->articleList) > 0)
       {
           echo '<ul>';
	       foreach ($this->articleList as $item)
	       {
	           echo '<li>' . $item->Subject . '</li>';
	       }
           echo '</ul>';
       }
       echo '</li>';   
    }
    
    function Serialize()
    {
        
    }
    
    
}